package com.lvchangqi.util;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

/**
 * Created by version on 17-5-4.
 */
@Component
public class SMSUtil {

    private final String appKey = "23794305";  //应用列表中可以找到
    private final String appSecret = "f5624195993fcf953947c6305cc43de1";//secret
    private final String url = "http://gw.api.taobao.com/router/rest";   //网址
    private final String smsTemplateCode0 = "SMS_75805071";    //模板ID
    private final String smsTemplateCode1 = "SMS_75705114";
    private final String smsFreeSignName = "吕长奇";    //短信签名

    /**
     * 发送验证码
     *
     * @param phone 手机号码
     * @param name  老师姓名
     * @param student 学生姓名
     */
    public Boolean sendSMStoTeacher(String phone, String name, String student, String phone_m) throws
            ApiException {
        String time = new SimpleDateFormat("MM月dd日 HH:mm").format(new Date());

        TaobaoClient client = new DefaultTaobaoClient(url, appKey, appSecret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
//            req.setExtend("extend");
        req.setSmsType("normal");  //短信类型
        req.setSmsFreeSignName(smsFreeSignName);
        req.setSmsParamString("{name:'" + name + "',student:'" + student + "',phone:'" + phone_m
                +"',time:'" + time + "'}");
        req.setRecNum(phone);
        req.setSmsTemplateCode(smsTemplateCode0);
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);

        return rsp.isSuccess();

    }

    public Boolean sendSMStoLeader(String phone, String name, String clazz, String student) throws
            ApiException {

        TaobaoClient client = new DefaultTaobaoClient(url, appKey, appSecret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
//            req.setExtend("extend");
        req.setSmsType("normal");  //短信类型
        req.setSmsFreeSignName(smsFreeSignName);
        req.setSmsParamString("{name:'" + name + "',student:'" + student + "',clazz:'" + clazz + "'}");
        req.setRecNum(phone);
        req.setSmsTemplateCode(smsTemplateCode1);
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);

        return rsp.isSuccess();

    }
}
